﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellPerformance_h
#define CellPerformance_h

#include "CellPrerequisites.h"
#include "CellThread.h"
#include "CellStringVector.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup Util
 *  @{
 */

/** 系统信息信息辅助类。
@author
	Jiangwei Xu
*/
class _CellExport Performance : public Thread
{
public:
	/**
	*/
	Performance(void);

	/**
	*/
	virtual ~Performance(void);

public:
	/**
	*/
	void run(void);

	/**
	*/
	void stop(void);

	/**
	*/
	void await(void);

	/**
	*/
	const String& getHostName(void) const;

	/**
	*/
	const StringVector& getIPAddressList(void) const;

	/**
	*/
	ulong getCurrentProcessId(void) const;

	/**
	*/
	int getProcessorNumber(void) const;

	/**
	*/
	uint64 getTotalVirtualMemory(void) const;

	/**
	*/
	uint64 getVirtualMemoryUsed(void) const;

	/**
	*/
	uint64 getTotalPhysicalMemory(void) const;

	/**
	*/
	uint64 getPhysicalMemoryUsed(void) const;

	/**
	*/
	int getProcessCPUUsage(void) const;

	/**
	*/
	uint64 getProcessVirtualMemoryUsed(void) const;

	/**
	*/
	uint64 getProcessPhysicalMemoryUsed(void) const;

private:
	/**
	*/
	void collectInfo(void);

	/**
	*/
	bool collectHostInfo(String& hostName, StringVector& addressList);

private:
	bool mSpinning;
	bool mRunning;

	String mHostName;
	StringVector mIPAddressList;
	int mNumberOfProcessors;
	ulong mProcessId;

	typedef std::list<int> UsageList;
	UsageList mCPUUsageList;

	uint64 mTotalVirtualMem;
	uint64 mVirtualMemUsed;
	uint64 mTotalPhysicalMem;
	uint64 mPhysicalMemUsed;

	uint64 mProcessVirtualMemUsed;
	uint64 mProcessPhysicalMemUsed;

#if CELL_PLATFORM == CELL_PLATFORM_WIN32
	// 上一次的时间
	uint64 mLastTime;
	uint64 mLastSystemTime;
#endif
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellPerformance_h
